package com.jike.jkgame;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;

import android.content.res.Resources;
import android.os.Environment;

public class Utilis {
	private Utilis() throws IllegalAccessException {
		throw new IllegalAccessException();
	}
	
	public static final String SEPERATOR = "#";
	public static String intArrayToString(int[] nums) {
		
		if (nums == null){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<nums.length; ++i) {
			sb.append(nums[i]);
			
			if (i != nums.length-1) {
				sb.append(SEPERATOR);
			}
		}
		
		return sb.toString();
	}
	
	public static int[] stringToIntArray(String str) {
		if (str == null) {
			return null;
		}
		
		String[] numStrs = str.split(SEPERATOR);
		int[] nums = new int[numStrs.length];
		for (int i=0; i<numStrs.length; ++i) {
			nums[i] = Integer.valueOf(numStrs[i]);
		}
		
		return nums;
		
	}
	
	public static <T> String arrayToString(T[] items) {
		if (items == null){
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<items.length; ++i) {
			sb.append(items[i].toString());
			
			if (i != items.length-1) {
				sb.append(SEPERATOR);
			}
		}
		
		return sb.toString();
	}
	
	public static File getAppDir() {
		File dir = Environment.getExternalStorageDirectory();
		dir = new File(dir, "JKGame");
		return dir;
	}
	
	public static File getTempDir() {
		File tmp = new File(getAppDir(), "tmp");
		return tmp;
	}
	
	public static String fromatTime(long seconds) {
		long h = seconds / 3600;
		long m = (seconds - h * 3600) / 60;
		long s = seconds % 60;
		
		return String.format("%d:%02d:%02d", h, m, s);
	}
	
	public static String getResString(int resId) {
	    return JKApp.get().getString(resId);
	}
	
	public static String[] getResStringArrary(int resId) {
	    Resources res = JKApp.get().getResources();
	    return res.getStringArray(resId);
	}
	
	public static String getString(DataInputStream dis, byte[] buff) throws IOException {
		int len = dis.readInt();
		dis.read(buff, 0, len);
		
		String str = new String(buff, 0, len-1, "utf-8");
		return str;
	}
	
}
